package com.micro.modual.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.common.MicroConstant;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.page.PageQuery;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonPageData;
import com.micro.modual.async.completion.manager.CompletionExecuteManager;
import com.micro.modual.entity.GoodsSku;
import com.micro.modual.extension.GoodsSkuExtension;
import com.micro.modual.mapper.GoodsSkuMapper;
import com.micro.modual.model.bo.goodssku.GoodsSkuBO;
import com.micro.modual.model.dto.goodssku.GoodsSkuListDTO;
import com.micro.modual.service.GoodsSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>商品sku表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("goodsSkuService")
public class GoodsSkuServiceImpl extends AbstractBasicsServiceImpl<GoodsSku,Long> implements GoodsSkuService {

    @Autowired
    private GoodsSkuMapper goodsSkuMapper;
    
    @Autowired
    private CompletionExecuteManager completionExecuteManager;

    /**
     * <h2>查找商品sku表 列表</h2>
     * @param params 列表查询请求对象
     * @return List<GoodsSkuListDTO> {@link GoodsSkuListDTO}s  商品sku表 列表返回对象
     */
    @Override
    public RespJsonPageData<GoodsSkuListDTO> findGoodsSkuList(Map<String, Object> params) {
        PageQuery pageQuery = new PageQuery(params);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<GoodsSku> goodsSkuList = goodsSkuMapper.findGoodsSkuList(params);
        List<GoodsSkuListDTO> goodsSkuDTOList = JacksonUtil.getList(JacksonUtil.toJson(goodsSkuList),GoodsSkuListDTO.class);
        return new RespJsonPageData<GoodsSkuListDTO>(goodsSkuDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());

    }
    
    /**
     * <h2>修改商品sku表详情</h2>
     * @param goodsSkuBO  {@link GoodsSkuBO} 商品sku表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editGoodsSku(GoodsSkuBO goodsSkuBO) {
        GoodsSku goodsSku = GoodsSkuExtension.converEditGoodsSku(goodsSkuBO);
        goodsSkuMapper.updateByPrimaryKeySelective(goodsSku);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除商品sku表信息</h2>
     * @param id  商品sku表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteGoodsSku(Long id) {
        GoodsSku goodsSku = goodsSkuMapper.selectByPrimaryKey(id);
        if(goodsSku.getGoodsStatus().equals(MicroConstant.StatusEffective.ENABLE)){
            throw new BizException(MicroResponseCode.GOODS_ON_SALE_NOT_ALLOW_DELETE.getResponseCode(),MicroResponseCode.GOODS_ON_SALE_NOT_ALLOW_DELETE.getResponseMessage());
        }
        completionExecuteManager.computeRule(MicroConstant.AsyncRuleTypeCode.ASYNC_REMOVE_GOODS_SKU,goodsSku);
        goodsSkuMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

   

}
